package com.study.springboottest.test;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SparseArray {

    public static void main(String[] args) {
        // 创建一个原始的二维数组 11*11
        // 0:表示没有棋子 1:表示黑子 2:表示白字
        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[5][2] = 5;
        System.out.println("原始的二维数组~");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        // 将原始数组转换为稀松数组
        // 1.遍历二维数组,得到非0数组的个数
        int sum = 0;
        for (int[] row : chessArr1) {
            for (int data : row) {
                if (data != 0) {
                    sum++;
                }
            }
        }

        // 2.创建对应的稀松数组
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        System.out.println("稀松数组~");
        for (int[] row : sparseArr) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        String fileName = "map.data";

        saveSparseArrayToFile(sparseArr, fileName);

        int[][] loadSparseArrayFromFile = loadSparseArrayFromFile(fileName);

        //将稀松数组恢复为原始数组
        int[][] chessArr2 = new int[loadSparseArrayFromFile[0][0]][loadSparseArrayFromFile[0][1]];
        for (int i = 1; i < loadSparseArrayFromFile.length; i++) {
            chessArr2[loadSparseArrayFromFile[i][0]][loadSparseArrayFromFile[i][1]] = loadSparseArrayFromFile[i][2];
        }
        System.out.println("新的二维数组~");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }

    public static void saveSparseArrayToFile(int[][] sparseArray, String fileName) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(fileName)));
            oos.writeObject(sparseArray);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static int[][] loadSparseArrayFromFile(String fileName) {
        int[][] loadedSparseArray = null;
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(Files.newInputStream(Paths.get(fileName)));
            loadedSparseArray = (int[][]) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return loadedSparseArray;
    }
}
